package com.bs.web.rest;

import com.bs.domain.Deploy;
import com.bs.service.DeployService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * REST controller for managing Deploy.
 */
@RestController
@RequestMapping("/api/deploy")
public class DeployResource {

    @Autowired
    private DeployService service;

    @GetMapping("/getPartitions")
    @ResponseBody
    public List<Deploy> getPartitions(){

        return service.getPartitions();
    }

    @PostMapping("/query")
    @ResponseBody
    public List<Deploy> query(@RequestBody Deploy deploy){

        return service.query(deploy);
    }

    @PostMapping("/create")
    @ResponseBody
    public String create(@RequestBody Deploy deploy, HttpServletResponse resp){
        try {
            Deploy deploy1=service.create(deploy);
            return deploy1.getId().toString();
        } catch (Exception e) {
            try {
                resp.sendError(403,e.getMessage());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }

    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Deploy deploy, HttpServletResponse resp){
        Map<String, Object> map=new HashMap<String, Object>();
        try {
            service.update(deploy);
            map.put("result", "success");
            return map;
        } catch (Exception e) {
            try {
                resp.sendError(403,e.getMessage());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            map.put("result", "error:"+e.getMessage());
            return map;
        }

    }


}
